package com.leijie.protobuf.client;

import com.leijie.protobuf.DataInfo;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.Random;

/**
 * @author LeiJie
 * @time 2020-4-29 10:23
 * @desc TODO
 */
public class AppClientHandle extends SimpleChannelInboundHandler<DataInfo.Body> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, DataInfo.Body msg) throws Exception {

    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        int divsor = new Random().nextInt(3);
        DataInfo.Body body = null;
        switch (divsor){
            case 0:
                DataInfo.People people = DataInfo.People.newBuilder().setName("张三").setAge(100).setAddresss("河南").build();
                body = DataInfo.Body.newBuilder().setPeople(people).setDataType(DataInfo.DataType.PEOPLE).build();
                break;
            case 1:
                DataInfo.Dog dog = DataInfo.Dog.newBuilder().setName("小狗").setColor("黄色").build();
                body = DataInfo.Body.newBuilder().setDog(dog).setDataType(DataInfo.DataType.DOG).build();
                break;
            case 2:
                DataInfo.Cat cat = DataInfo.Cat.newBuilder().setName("小猫").setHobby("吃鱼").build();
                body = DataInfo.Body.newBuilder().setCat(cat).setDataType(DataInfo.DataType.CAT).build();
                break;
            default:
        }
        ctx.writeAndFlush(body);
    }
}
